﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using SMTC.Model.TestCore;
using SMTC.WebCommon.Common;
using SMTC.BO.Employee;

public partial class UserPower_UpLoadEmployee : BasePage
{
    private EmployeeQueryServices _employeequeryBo = new EmployeeQueryServices();
    EmployeeProccessServices _services = new EmployeeProccessServices();
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btn_Upload_Click(object sender, EventArgs e)
    {
        int index = UP_ImportEmployee.FileName.LastIndexOf(".");
        string extname = UP_ImportEmployee.FileName.Substring(index + 1).ToUpper();
        if (extname == "XLSX" || extname == "XLS")
        {
            DataSet ds = UP_ImportEmployee.AnalyzeExcelToDS();
            List<EmpInfo> list = new List<EmpInfo>();
            List<EmpInfo> oldlist = new List<EmpInfo>();
            List<EmpInfo> newlist = new List<EmpInfo>();
            if (ds.Tables[0].Rows.Count == 0)
                ShowMessage("Excel中无数据！操作无效！");//Excel无数据
            else
            {

                DataTable tb = ds.Tables[0];
                int j = 0;
                if (tb.Columns.Count >= 9)
                {
                    for (int i = 0; i < tb.Rows.Count; i++)
                    {
                        EmpInfo emp = new EmpInfo();
                        emp.EmpNbr = tb.Rows[i][j].ToString().Trim();
                        emp.EmpName = tb.Rows[i][j + 1].ToString().Trim();
                        emp.EmpDep = tb.Rows[i][j + 2].ToString().Trim();
                        emp.Position = tb.Rows[i][j + 3].ToString().Trim();
                        emp.ExtNum = tb.Rows[i][j + 4].ToString().Trim();
                        emp.MTel = tb.Rows[i][j + 5].ToString().Trim();
                        emp.EMail = tb.Rows[i][j + 6].ToString().Trim();
                        emp.Fax = tb.Rows[i][j + 7].ToString().Trim();
                        emp.Remark = tb.Rows[i][j + 8].ToString().Trim();
                        list.Add(emp);

                    }

                    try
                    {
                        foreach (EmpInfo em in list)//筛选员工编号，姓名，部门不能为空
                        {
                            if (em.EmpNbr != "" && em.EmpName != "" && em.EmpDep != "")
                            {
                                oldlist.Add(em);
                            }
                        }
                        foreach (EmpInfo em in oldlist)//如数据库中已存相同员工编号，则过虑现在有
                        {
                            if (_employeequeryBo.ExistEmployee(em.EmpNbr) == false)
                                newlist.Add(em);

                        }
                        if (newlist.Count == 0)
                        {
                            ShowMessage("有效数据为0!操作无效");
                        }
                        else
                        {
                            bool result = _services.Insert_Llist_Employee(newlist);
                            if (result == true)
                            {
                                ShowMessage("上传成功");
                            }
                            else
                            {
                                ShowMessage("上传失败");
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw new Exception(ex.Message);
                    }
                }
                else
                {
                    ShowMessage("Excel中数据列数与模板不符！操作无效！");
                }
            }
        }
        else
        {
            ShowMessage("非Excel文件！操作无效！");
        
        }
    }
}